//Demo
layui.use(['form', 'laydate', 'upload'], function () {
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;
    let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    $(function () {
        getCode();
        getRoleList();
        $("#code").val(userInfo.code);
        $("#name").val(userInfo.name);
        $("#phone").val(userInfo.phone);
        $("#address").val(userInfo.address);
        $("#sex").find("option[value='" + userInfo.sex + "']").prop("selected", true);
        $("#graduate").find("option[value='" + userInfo.graduate + "']").prop("selected", true);
        $("#role").find("option[value='" + userInfo.role + "']").prop("selected", true);
        $("#entranceTime").val(userInfo.entranceTime);
        $("#icon").attr("src", userInfo.icon);
        $("#role").attr("disabled","disabled");
        form.render();
    })

    $("#update").click(function () {
        let password = $("#password").val();
        let cfmpsd = $("#cfmpassword").val();
        if (password != cfmpsd) {
            layer.msg("两次密码输入不一致", function () {
                $("#password").focus();
            })
        } else {
            let userdata = {
                "id": userInfo.id,
                "code": $("#code").val(),
                "name": $("#name").val(),
                "password": $("#password").val(),
                "phone": $("#phone").val(),
                "address": $("#address").val(),
                "sex": $("#sex").find("option:selected").val(),
                "graduate": $("#graduate").find("option:selected").val(),
                "role": $("#role").find("option:selected").val(),
                "entranceTime": $("#entranceTime").val(),
                "icon": $("#icon").attr("src")
            };
            let authorities = userInfo.authorities;
            console.log(userdata);
            let result = ajaxModel("/user/user/updateUser", "post", userdata);
            if (result.code == 200) {
                layer.alert(result.message, {icon: 1}, function (index) {
                    result.data["authorities"]=authorities;
                    sessionStorage.setItem("userInfo", JSON.stringify(result.data));
                    layer.close(index);
                })
            } else
                layer.alert(result.message, {icon: 1});
        }
    });
    $("#cancel").click(function () {
        layer.close(index);
    });
    form.render();
    laydate.render({
        elem: '#entranceTime'
    })
    upload.render({
        elem: '#icon_change', //绑定元素

        url: LOCALHOSTURL + '/user/upload', //上传接口

        accept: 'images',
        acceptMime: 'image/*',
        xhrFields: {
            withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
        },
        headers: {
            "Authorization": $.cookie("token")
        },
        crossDomain: true,
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#icon').attr('src', result); //图片链接（base64）
            });
        },
        error: function () {
        }
    })

    //获取码表数据
    function getCode() {
        let data = null;
        if (sessionStorage.getItem("code") != null) {
            data = JSON.parse(sessionStorage.getItem("code"));
        } else {
            let result = ajaxModel("/user/user/getCodeList", "get");
            data = result.data;
            sessionStorage.setItem("code", JSON.stringify(data));
        }
        console.log(data);
        $("#sex").html("<option value=''>全部</option>");
        $.each(data.sex, function (i, sex) {
            $("#sex").append("<option value='" + sex.code + "'>" + sex.name + "</option>");
        })
        $("#graduate").html("<option value=''>全部</option>");
        $.each(data.graduate, function (i, graduate) {
            $("#graduate").append("<option value='" + graduate.code + "'>" + graduate.name + "</option>");
        })
    }

    function getRoleList() {
        let data = [];
        if (sessionStorage.getItem("role") != null) {
            data = JSON.parse(sessionStorage.getItem("role"));
        } else {
            let result = ajaxModel("/role/role/getRoleList", "get");
            data = result.data;
            sessionStorage.setItem("role", JSON.stringify(data));
        }
        $("#role").html("<option value=''>全部</option>");
        $.each(data, function (i, role) {
            $("#role").append("<option value='" + role.id + "'>" + role.name + "</option>");
        });
    }
});
